def modules = [
        [
                moduleName    : "docker-bi",
                dockerfilePath: "docker/iap/dockerfile-openjdk-jre",
                dockerfile    : "dockerfile-openjdk-jre",
                jarPath       : "modules/bi/target/modules-bi.jar",
                imageName     : "isap-modules-bi",
                tagName       : "latest",
                containerName : "isap-modules-bi",
                JVM_OPT       : "-Xmx1024m -Xms512m",
                HOST_PORT     : "9001",
                CONTAINER_PORT: "9001",
                SSH_HOST      : "192.168.0.4",
                SSH_PORT      : "22",
        ],
        [
                moduleName    : "docker-record",
                dockerfilePath: "docker/iap/dockerfile-openjdk-jre",
                dockerfile    : "dockerfile-openjdk-jre",
                jarPath       : "modules/record/target/modules-record.jar",
                imageName     : "isap-modules-record",
                tagName       : "latest",
                containerName : "isap-modules-record",
                JVM_OPT       : "-Xmx1024m -Xms512m",
                HOST_PORT     : "9002",
                CONTAINER_PORT: "9002",
                SSH_HOST      : "192.168.0.4",
                SSH_PORT      : "22",
        ],
        [
                moduleName    : "docker-analysis",
                dockerfilePath: "docker/iap/dockerfile-openjdk-jre",
                dockerfile    : "dockerfile-openjdk-jre",
                jarPath       : "modules/analysis/target/modules-analysis.jar",
                imageName     : "isap-modules-analysis",
                tagName       : "latest",
                containerName : "isap-modules-analysis",
                JVM_OPT       : "-Xmx1024m -Xms512m",
                HOST_PORT     : "9003",
                CONTAINER_PORT: "9003",
                SSH_HOST      : "192.168.0.4",
                SSH_PORT      : "22",
        ],
        [
                moduleName    : "docker-tenant",
                dockerfilePath: "docker/iap/dockerfile-openjdk-jre",
                dockerfile    : "dockerfile-openjdk-jre",
                jarPath       : "modules/tenant/target/modules-tenant.jar",
                imageName     : "isap-modules-tenant",
                tagName       : "latest",
                containerName : "isap-modules-tenant",
                JVM_OPT       : "-Xmx1024m -Xms512m",
                HOST_PORT     : "9004",
                CONTAINER_PORT: "9004",
                SSH_HOST      : "192.168.0.4",
                SSH_PORT      : "22",
        ],
        [
                moduleName    : "docker-system",
                dockerfilePath: "docker/iap/dockerfile-openjdk-jre",
                dockerfile    : "dockerfile-openjdk-jre",
                jarPath       : "modules/system/target/modules-system.jar",
                imageName     : "isap-modules-system",
                tagName       : "latest",
                containerName : "isap-modules-system",
                JVM_OPT       : "-Xmx1024m -Xms512m",
                HOST_PORT     : "9005",
                CONTAINER_PORT: "9005",
                SSH_HOST      : "192.168.0.4",
                SSH_PORT      : "22",
        ],
        [
                moduleName    : "docker-sale",
                dockerfilePath: "docker/iap/dockerfile-openjdk-jre",
                dockerfile    : "dockerfile-openjdk-jre",
                jarPath       : "modules/sale/target/modules-sale.jar",
                imageName     : "isap-modules-sale",
                tagName       : "latest",
                containerName : "isap-modules-sale",
                JVM_OPT       : "-Xmx1024m -Xms512m",
                HOST_PORT     : "9006",
                CONTAINER_PORT: "9006",
                SSH_HOST      : "192.168.0.5",
                SSH_PORT      : "22",
        ],
        [
                moduleName    : "docker-trial",
                dockerfilePath: "docker/iap/dockerfile-openjdk-jre",
                dockerfile    : "dockerfile-openjdk-jre",
                jarPath       : "modules/trial/target/modules-trial.jar",
                imageName     : "isap-modules-trial",
                tagName       : "latest",
                containerName : "isap-modules-trial",
                JVM_OPT       : "-Xmx512m -Xms512m",
                HOST_PORT     : "9007",
                CONTAINER_PORT: "9007",
                SSH_HOST      : "192.168.0.5",
                SSH_PORT      : "22",
        ],
        [
                moduleName    : "docker-gateway",
                dockerfilePath: "docker/iap/dockerfile-openjdk-jre",
                dockerfile    : "dockerfile-openjdk-jre",
                jarPath       : "gateway/target/gateway.jar",
                imageName     : "isap-gateway",
                tagName       : "latest",
                containerName : "isap-gateway",
                JVM_OPT       : "-Xmx1024m -Xms512m",
                HOST_PORT     : "8080",
                CONTAINER_PORT: "8080",
                SSH_HOST      : "192.168.0.5",
                SSH_PORT      : "22",
        ],
        [
                moduleName    : "docker-auth",
                dockerfilePath: "docker/iap/dockerfile-openjdk-jre",
                dockerfile    : "dockerfile-openjdk-jre",
                jarPath       : "auth/target/auth.jar",
                imageName     : "isap-auth",
                tagName       : "latest",
                containerName : "isap-auth",
                JVM_OPT       : "-Xmx512m -Xms512m",
                HOST_PORT     : "9009",
                CONTAINER_PORT: "9009",
                SSH_HOST      : "192.168.0.5",
                SSH_PORT      : "22",
        ],
        [
                moduleName    : "docker-iap",
                dockerfilePath: "docker/iap/dockerfile-iap",
                dockerfile    : "dockerfile-iap",
                jarPath       : "modules/iap/target/modules-iap.jar",
                imageName     : "isap-modules-iap",
                tagName       : "latest",
                containerName : "isap-modules-iap",
                JVM_OPT       : "-Xmx1024m -Xms512m",
                HOST_PORT     : "9010",
                CONTAINER_PORT: "9010",
                SSH_HOST      : "192.168.0.5",
                SSH_PORT      : "22",
        ],
        [
                moduleName    : "docker-iot",
                dockerfilePath: "docker/iap/dockerfile-iot",
                dockerfile    : "dockerfile-iot",
                jarPath       : "modules/iot/target/modules-iot.jar",
                imageName     : "isap-modules-iot",
                tagName       : "latest",
                containerName : "isap-modules-iot",
                JVM_OPT       : "-Xmx512m -Xms512m",
                HOST_PORT     : "9011",
                CONTAINER_PORT: "9011",
                SSH_HOST      : "192.168.0.5",
                SSH_PORT      : "22",
        ]

]


pipeline {
    agent any

    tools {
        maven "maven-3.9.5"
    }
    parameters {
        choice(name: 'build_image_name'
                , choices: ['all', 'isap-modules-iot', 'isap-modules-bi', 'isap-modules-record', 'isap-modules-analysis', 'isap-modules-tenant', 'isap-modules-system', 'isap-modules-sale', 'isap-modules-trial', 'isap-gateway', 'isap-auth', 'isap-modules-iap']
                , description: '选择要构建的镜像')
    }
    environment {
        ENV_REGISTRY = 'aibharbor.newtouch.com'
        ENV_PROJECT = 'isap'
    }
    stages {
        stage('Build Java') {
            steps {
                sh 'echo Start Build Java'
                git branch: 'dev', credentialsId: '3bd32fb2-15ed-450e-b477-198d53eeb207', url: 'http://git.runlin.cn/bleach_song/isap.git'
                sh "mvn -Dmaven.test.failure.ignore=true clean package -DskipTests=true"
            }
            post {
                success {
                    echo 'mvn 打包 成功'
                }
                failure {
                    echo 'mvn 打包 失败'
                }
            }
        }
        stage('Docker Login') {
            steps {
                withCredentials([usernamePassword(credentialsId: '95aa7525-d5ec-4d80-a660-bc633ec00ccf', usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) {
                    sh "docker login --username $DOCKER_USERNAME --password $DOCKER_PASSWORD ${ENV_REGISTRY}"
                }
            }
            post {
                success {
                    echo 'Docker Login Success'
                }
                failure {
                    echo 'Docker Login Failure'
                }
            }
        }
        stage('Docker Build & Push') {
            steps {
                script {
                    sh "echo 开始构建docker镜像,获取到的构建参数： ${params.build_image_name}"
                    def buildImageName = params.build_image_name
                    def buildModules = modules
                    if (buildImageName != 'all') {
                        // 通过 build_image_name 来进行过滤重新得到 modules
                        buildModules = modules.findAll { module ->
                            return module.imageName == buildImageName
                        }
                    }

                    parallel buildModules.collectEntries { module ->
                        ["${module.moduleName}": {
                            def jarPath = module.jarPath
                            def imageName = module.imageName
                            def imageTag = module.tagName
                            def dockerfilePath = module.dockerfilePath
                            def dockerfile = module.dockerfile
                            dir("docker/iap/${module.moduleName}") {
                                sh """
                                    echo '开始构建镜像：${imageName}'
                                    rm -rf *.jar
                                    cp $WORKSPACE/$jarPath ./
                                    cp $WORKSPACE/$dockerfilePath ./
                                    cp ../docker-entrypoint.sh ./
                                    cp ../bootstrap.yml ./
                                    ls -al
                                    docker build -f ${dockerfile} --build-arg JARNAME=app-${BUILD_NUMBER}.jar -t "${ENV_REGISTRY}/${ENV_PROJECT}/${imageName}:${imageTag}" .
                                    docker push "${ENV_REGISTRY}/${ENV_PROJECT}/${imageName}:${imageTag}"
                                    docker rmi "${ENV_REGISTRY}/${ENV_PROJECT}/${imageName}:${imageTag}"
                                """
                            }
                        }]
                    }

                }
            }
            post {
                success {
                    echo 'Docker Build Success'
                }
                failure {
                    echo 'Docker Build Failure'
                }
            }
        }
        stage('Deploy Docker Container') {
            steps {
                script {
                    sh "echo 开始部署docker容器...,获取到的构建参数： ${params.build_image_name}"
                    def buildImageName = params.build_image_name
                    def buildModules = modules
                    if (buildImageName != 'all') {
                        // 通过 build_image_name 来进行过滤重新得到 modules
                        buildModules = modules.findAll { module ->
                            return module.imageName == buildImageName
                        }
                    }

                    parallel buildModules.collectEntries { module ->
                        ["${module.moduleName}": {
                            def image = "${ENV_REGISTRY}/${ENV_PROJECT}/${module.imageName}:${module.tagName}"
                            def containerName = module.containerName
                            def NACOS_IP = '192.168.0.8'
                            def NACOS_PORT = '8848'
                            def NACOS_NS = 'public'
                            def NACOS_GROUP = 'DEFAULT_GROUP'
                            def NACOS_USERNAME = 'nacos'
                            def NACOS_PASSWORD = 'p@ssw0rd'
                            // 容器网络
                            def NETWORK = 'isap'

                            def EXPOSE_PORT = "-p ${module.HOST_PORT}:${module.CONTAINER_PORT}"
                            def ENV_APP_OPT = "--server.port=${module.CONTAINER_PORT} --spring.cloud.nacos.discovery.ip=${module.SSH_HOST} --spring.cloud.nacos.discovery.port=${module.HOST_PORT}"
                            def ENV_JVM_OPT = module.containsKey('JVM_OPT') ? module.get('JVM_OPT') : ''


                            def SSH_HOST = module.SSH_HOST
                            def SSH_PORT = module.SSH_PORT
                            sh """
                                ssh -p $SSH_PORT root@$SSH_HOST 'if [ "\$(docker network ls | grep " $NETWORK " -c)" = "0" ];then echo "创建需要使用的network..." && docker network create $NETWORK ;else  echo "已存在需要的network...";fi'
                                ssh -p $SSH_PORT root@$SSH_HOST 'if [ "\$(docker ps -a | grep $containerName -c)" = "0" ];then echo "没有要关闭的 $containerName 容器";else  echo "有要关闭的 $containerName 容器" && docker stop $containerName && docker rm $containerName;fi'
                                ssh -p $SSH_PORT root@$SSH_HOST 'docker run -itd --name ${containerName} --restart always -e TZ="Asia/Shanghai" --network ${NETWORK} ${EXPOSE_PORT} -e ENV_APP_OPT="${ENV_APP_OPT}" -e ENV_JVM_OPT="${ENV_JVM_OPT}" -e NACOS_IP=${NACOS_IP} -e NACOS_PORT=${NACOS_PORT} -e NACOS_NS=${NACOS_NS} -e NACOS_GROUP=${NACOS_GROUP} -e NACOS_USERNAME=${NACOS_USERNAME} -e NACOS_PASSWORD=${NACOS_PASSWORD}  ${image}'
"""

                        }]
                    }
                }
            }
            post {
                success {
                    echo 'Deploy Docker Container Success'
                }
                failure {
                    echo 'Deploy Docker Container Failure'
                }
            }
        }
    }
}